 # -*- coding: utf8 -*-



from django.db.models import Model, CharField, DateField, IntegerField
from django.db.models import  AutoField, ForeignKey, URLField, ManyToManyField 


from datetime import datetime

class Region(Model):
	class Meta:
		verbose_name = u"Kraj"
		verbose_name_plural = u"Kraje"
	name = CharField(verbose_name = u"Názov", max_length = 255)

	def __unicode__(self) :
		return self.name


class City(Model):
	class Meta:
		verbose_name = u"Mesto"
		verbose_name_plural = u"Mestá"
	name = CharField(verbose_name = u"Názov", max_length = 255)
	#region = ForeignKey(Region, verbose_name = "Kraj")

	def __unicode__(self) :
		return self.name


class Address(Model):
	class Meta:
		verbose_name = u"Adresa"
		verbose_name_plural = u"Adresy"
			
	id = AutoField(verbose_name = u"Interné id", primary_key = True)
	street = CharField(verbose_name = u"Ulica", max_length = 255)
	number = CharField(verbose_name = u"Popisné číslo", max_length = 16)
	city = ForeignKey(City, verbose_name = u"Mesto")
	zip_code = CharField(verbose_name = u"PSČ", max_length = 16)

	def dump(self):
		return u"{%s %s, %s %s}" % (self.street, self.number, self.city, self.zip_code)

	def __unicode__(self):
		return u"%s %s, %s %s" % (self.street, self.number, self.city, self.zip_code)
		

class Company_Type(Model):
	class Meta:
		verbose_name = u"Právna forma"
		verbose_name_plural = u"Právne formy"
		
	id = AutoField(verbose_name = u"Interné id", primary_key = True)
	type = CharField(verbose_name = u"Právna forma", max_length = 255)

	def __unicode__(self):
		return u'%s' % self.type 

class Company_Business(Model):
	class Meta:
		verbose_name = u"Predmet podnikania"
		
	id = AutoField(verbose_name = u"Interné id", primary_key = True)
	business = CharField(verbose_name = u"Predmet podnikania", max_length = 512)
	icon = CharField(max_length = 512)

	def __unicode__(self):
		return u'%s' % self.business	


class Company_Position(Model):
	id = AutoField(verbose_name = u"Interné id", primary_key = True)
	position = CharField(verbose_name = u"Pozícia", max_length = 255)

	
class Person(Model):
	class Meta:
		verbose_name = u"Podnikateľ"
		verbose_name_plural = u"Podnikatelia"
	
	id = AutoField(verbose_name = u"Interné id", primary_key = True)
	position = CharField(verbose_name = u"Pozícia", max_length = 255)
	title = CharField(verbose_name = u"Titul", max_length = 16)
	first_name = CharField(verbose_name = u"Meno", max_length = 255)
	last_name = CharField(verbose_name = u"Priezvisko", max_length = 255)	
	address = ForeignKey(Address, verbose_name = u"Bydlisko")
	
	def __unicode__(self):
		return u'%s %s %s (Bydlisko: %s)' % (self.title, self.first_name, self.last_name, self.address)
	

class Company(Model):
	class Meta:
		verbose_name = u"Firma"
		verbose_name_plural = u"Firmy"
		ordering = ["-created"]
	
	id = AutoField(verbose_name = u"Interné id", primary_key = True)
	name = CharField(verbose_name = u"Obchodné meno", max_length = 255)	
	address = ForeignKey(Address, verbose_name = "Sídlo")
	company_id = CharField(verbose_name = u"IČO", max_length = 16)
	company_type = ForeignKey(Company_Type, verbose_name = "Právna forma")
	business_list = ManyToManyField(Company_Business, verbose_name = u"Predmet podnikania")
	person_list = ManyToManyField(Person, verbose_name = u"Podnikatelia")	
	url = URLField(verbose_name = u"Odkaz na obchodný register")
	created = DateField(verbose_name = "Dátum zápisu do OR") 
	
	def dump(self):
		return u"company: {id = %d, name = %s, address = %s, ICO = %s}" % (self.id, self.name, self.address.dump(), self.company_id)
				
	def __unicode__(self) :
		return u'%s (Sídlo: %s)' % (self.name, self.address)
	
	
class Company_Person_Map(Model):
	class Meta:
		verbose_name = u"Asociácia titulu, mena a priezviska podnikateľa s firmou"
		verbose_name_plural = u"Asociácia titulu, mena a priezviska podnikateľa s firmami"
	
	company_name = CharField(verbose_name = u"Obchodné meno", max_length = 255)
	company_url = URLField(verbose_name = u"Odkaz na úplny výpis firmy z obchodného registra")
	person_title = CharField(verbose_name = u"Titul", max_length = 16)
	person_first_name = CharField(verbose_name = u"Meno", max_length = 255)
	person_last_name = CharField(verbose_name = u"Priezvisko", max_length = 255)

	def __unicode__(self) :
		return u'%s %s %s - %s' % (self.person_title, self.person_first_name, self.person_last_name, self.company)

		
class Company_Person_Stat(Model):
	class Meta:
		verbose_name_plural = u"Počet firiem, v kt. figuruje meno a priezvisko podnikateľa"		
	person_first_name = CharField(verbose_name = u"Meno", max_length = 255)
	person_last_name = CharField(verbose_name = u"Priezvisko", max_length = 255)
	num_companies = IntegerField(verbose_name = u"Počet firiem")

	def __unicode__(self) :
		return u'%s %s - %d' % (self.person_first_name, self.person_last_name, self.num_companies) 